package com.mr.wordcount;

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class MTJoinMain extends Configured implements Tool {


    public static class Map extends Mapper<Object, Text, Text, Text> {


        @Override
        protected void map(Object key, Text value, Context context) throws IOException, InterruptedException {
            String line = value.toString();
            String num = line.substring(0, 1);

            System.out.println(line);
            if (StringUtils.isNumeric(num)) {
                context.write(new Text(num.trim()), new Text(line.substring(2)));
            } else {
                num = line.substring(line.trim().length() - 1);
                System.out.println(num);
                String name = line.substring(0, line.length() - 2);
                context.write(new Text(num.trim()), new Text(name + ":"));
            }

        }
    }

    public static class Reduce extends Reducer<Text, Text, Text, Text> {

        @Override
        protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            List<String> names = new ArrayList<String>();
            List<String> adrs = new ArrayList<String>();


            for (Text value : values) {
                System.out.println(key + " " + value);
                String v = value.toString();
                if (v.endsWith(":")) {
                    names.add(v);
                } else {
                    adrs.add(v);
                }
            }

            for (String child : names) {
                for (String parent : adrs) {
                    System.out.println(child + " " + parent);
                    context.write(new Text(child), new Text(parent));
                }
            }

        }
    }


    public int run(String[] args) throws Exception {
        Job job = new Job(getConf());
        job.setJarByClass(MTJoinMain.class);
        job.setJobName("StJoinMain");


        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);


        job.setMapperClass(MTJoinMain.Map.class);
        job.setReducerClass(MTJoinMain.Reduce.class);

        job.setInputFormatClass(TextInputFormat.class);
        job.setOutputFormatClass(TextOutputFormat.class);


        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        boolean success = job.waitForCompletion(true);
        return success ? 0 : 1;
    }

    public static void main(String[] args) throws Exception {
        int res = ToolRunner.run(new MTJoinMain(), args);
        System.exit(res);
    }
}
